/*
 * mainframe.cpp
 *
 *  Created on: 2012-8-22
 *      Author: Yangde
 */

#include "mainframe.h"
#include "scedit.h"
#include "wx/filedlg.h"
#include "../data/RecentFiles.h"

BEGIN_EVENT_TABLE(MainFrame, wxFrame)
    EVT_ERASE_BACKGROUND(MainFrame::OnEraseBackground)
    EVT_SIZE(MainFrame::OnSize)

    EVT_MENU (wxID_NEW,              MainFrame::OnNewTab)
    EVT_MENU (wxID_OPEN,             MainFrame::OnFileOpen)

    EVT_MENU(wxID_EXIT, MainFrame::OnExit)
    EVT_MENU(wxID_ABOUT, MainFrame::OnAbout)
END_EVENT_TABLE()


MainFrame::MainFrame(wxWindow* parent,
                 wxWindowID id,
                 const wxString& title,
                 const wxPoint& pos,
                 const wxSize& size,
                 long style)
        : wxFrame(parent, id, title, pos, size, style)
{
    // tell wxAuiManager to manage this frame
    m_mgr.SetManagedWindow(this);

    // min size for the frame itself isn't completely done.
    // see the end up wxAuiManager::Update() for the test
    // code. For now, just hard code a frame minimum size
    SetMinSize(wxSize(400,300));

    auiMenuBar* mb = new auiMenuBar(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
            wxAUI_TB_DEFAULT_STYLE );

    auiMenu* file_menu= new auiMenu();
    file_menu->Append(wxID_NEW, _("New"),wxArtProvider::GetBitmap(wxART_NEW, wxART_OTHER, wxSize(16,16)));
    file_menu->Append(wxID_OPEN, _("Open"),wxArtProvider::GetBitmap(wxART_FILE_OPEN, wxART_OTHER, wxSize(16,16)));
    file_menu->AppendSeparator();
    file_menu->Append(wxID_EXIT, _("Exit"));

    auiMenu* help_menu= new auiMenu();
    help_menu->Append(wxID_HELP, wxString("Help"));
    help_menu->Append(wxID_ABOUT, wxString("About"));

    mb->Append(file_menu, wxT("&File"));
    mb->Append(help_menu, wxT("&Edit"));
    mb->Realize();

    AttachMenuBar(mb);


    // create some toolbars
    wxAuiToolBar* tb1 = new wxAuiToolBar(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
                                         wxAUI_TB_DEFAULT_STYLE );

    tb1->AddTool(wxID_NEW, wxT("New"), wxArtProvider::GetBitmap(wxART_NEW, wxART_OTHER, wxSize(16,16)));
    tb1->AddTool(wxID_OPEN, wxT("Open"), wxArtProvider::GetBitmap(wxART_FILE_OPEN, wxART_OTHER, wxSize(16,16)));

    tb1->Realize();

    // create some center panes

    m_main_tab = CreateNotebook();
    m_mgr.AddPane(m_main_tab, wxAuiPaneInfo().Name(wxT("notebook_content")).
                  CenterPane().PaneBorder(0));

    // add the toolbars to the manager
    m_mgr.AddPane(mb, wxAuiPaneInfo().
                      Name(wxT("mb")).Caption(wxT("Menubar")).
                      ToolbarPane().Top());
    m_mgr.AddPane(tb1, wxAuiPaneInfo().
                  Name(wxT("tb1")).Caption(wxT("Toolbar[File]")).
                  ToolbarPane().Top().Row(1));


    // make some default perspectives

    // "commit" all changes made to wxAuiManager
    m_mgr.SetExBorder(1);
    m_mgr.Update();

}

MainFrame::~MainFrame()
{
    m_mgr.UnInit();
}


void MainFrame::OnEraseBackground(wxEraseEvent& event)
{
    event.Skip();
}

void MainFrame::OnSize(wxSizeEvent& event)
{
    event.Skip();
}


void MainFrame::OnExit(wxCommandEvent& WXUNUSED(event))
{
    Close(true);
}

void MainFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
    wxMessageBox(_("EasyEdit\nA light and easy text editor, A light and easy text editor\n(c) Copyright 2010-2012, Dean Yang"), _("About EasyEdit"), wxOK, this);
}


void MainFrame::AttachMenuBar(auiMenuBar* mebubar)
{
	m_menubar = mebubar;
}


wxAuiNotebook* MainFrame::CreateNotebook()
{
   // create the notebook off-window to avoid flicker
   wxSize client_size = GetClientSize();

   wxAuiNotebook* ctrl = new wxAuiNotebook(this, wxID_ANY,
                                    wxPoint(client_size.x, client_size.y),
                                    wxSize(430,200),
                                    wxAUI_NB_DEFAULT_STYLE | wxAUI_NB_TAB_EXTERNAL_MOVE | wxNO_BORDER);
   ctrl->Freeze();

   wxBitmap page_bmp = wxArtProvider::GetBitmap(wxART_NORMAL_FILE, wxART_OTHER, wxSize(16,16));




   int recent_cnt = recent_open.getRecentFileCnt();
   if(recent_cnt<=0)
   {
	   ScEdit* m_edit = new ScEdit (ctrl, wxID_ANY);
	   m_edit->SetSelection(0,0);
	   ctrl->AddPage( m_edit , wxEmptyString, false, page_bmp );
   }
   for(int i=0;i<recent_cnt;i++)
   {
	   wxFileName fname(recent_open.r_file_List.Item(i));
	   if(fname.Exists())
	   {
		   ScEdit* m_edit = new ScEdit (ctrl, wxID_ANY);
		   m_edit->LoadFile (recent_open.r_file_List.Item(i));
		   m_edit->SetSelection(0,0);
		   ctrl->AddPage( m_edit , fname.GetFullName(), false, page_bmp );
	   }
   }

   ctrl->Thaw();
   return ctrl;
}

void MainFrame::OnNewTab(wxCommandEvent& WXUNUSED(evt))
{
	wxBitmap page_bmp = wxArtProvider::GetBitmap(wxART_NORMAL_FILE, wxART_OTHER, wxSize(16,16));

	ScEdit* m_edit = new ScEdit (m_main_tab, wxID_ANY);
	m_edit->SetSelection(0,0);
	m_main_tab->AddPage( m_edit ,wxEmptyString, true, page_bmp );
}


void MainFrame::OnFileOpen(wxCommandEvent& WXUNUSED(evt))
{
    wxString fname;
    wxFileDialog dlg (this, wxT("Open file"), wxEmptyString, wxEmptyString, wxT("Any file (*)|*"),
                      wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_CHANGE_DIR);
    if (dlg.ShowModal() != wxID_OK) return;
    fname = dlg.GetPath ();
    FileOpen (fname);
}

void MainFrame::FileOpen (wxString fname)
{
	wxBitmap page_bmp = wxArtProvider::GetBitmap(wxART_NORMAL_FILE, wxART_OTHER, wxSize(16,16));

    wxFileName w(fname);
    w.Normalize();
    fname = w.GetFullPath();

	ScEdit* m_edit = new ScEdit (m_main_tab, wxID_ANY);
	m_edit->LoadFile (fname);
	m_edit->SetSelection(0,0);
	m_main_tab->AddPage( m_edit ,w.GetName(), true, page_bmp );
    recent_open.AddRecentFile(fname);
}

#define WM_SYSKEYDOWN 260
#define WM_SYSKEYUP 261
#define WM_KEYDOWN 256

bool MainFrame::MSWTranslateMessage(WXMSG *msg)
{
	unsigned int msgid = msg->message;

	if(msgid == WM_SYSKEYDOWN||msgid == WM_SYSKEYUP)
	{
		m_menubar->ProcessSysMsg(msg);
	}

    return false;
}
